﻿using ComponentFactory.Krypton.Toolkit;
using Nini.Config;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace TfsHelper.WinFrontEnd
{
    public partial class frmConfiguration : KryptonForm
    {
        #region " Fields "

        private IConfigSource config = new XmlConfigSource(Application.StartupPath + "\\Config.xml");

        #endregion

        #region " Constructor "

        public frmConfiguration(IList<Model.Workitem> colWi)
        {
            InitializeComponent();

            cmbWorkitems.DataSource = colWi;
            if (cmbWorkitems.Items.Count > 0)
                cmbWorkitems.SelectedIndex = 0;

            LoadConfig();
        }

        #endregion

        #region " Handlers "

        private void btnSaveConfiguration_Click(object sender, EventArgs e)
        {
            if (chkAutoStartMeeting.Checked && cmbWorkitems.SelectedIndex == -1)
            {
                KryptonMessageBox.Show("You must select a workitem to track effort.", "Auto Start", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            config.Configs["Meetings"].Set("Autostart", chkAutoStartMeeting.Checked.ToString());
            config.Configs["Meetings"].Set("WorkitemId", cmbWorkitems.SelectedValue.ToString());
            config.Configs["Meetings"].Set("Autostop", chkAutoStopMeeting.Checked.ToString());
            config.Configs["Appearance"].Set("Transparency", numTransparency.Value.ToString());
            config.Configs["Appearance"].Set("TopMost", chkTopMost.Checked.ToString());

            config.Save();

            KryptonMessageBox.Show("Configuration has been saved correctly.", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information);

            Close();
        }

        #endregion

        #region " Methods "

        private void LoadConfig()
        {
            chkAutoStartMeeting.Checked = config.Configs["Meetings"].GetBoolean("Autostart");
            if (chkAutoStartMeeting.Checked)
                cmbWorkitems.SelectedValue = config.Configs["Meetings"].GetInt("WorkitemId");
            chkAutoStopMeeting.Checked = config.Configs["Meetings"].GetBoolean("Autostop");

            numTransparency.Value = decimal.Parse(config.Configs["Appearance"].Get("Transparency"));
            chkTopMost.Checked = config.Configs["Appearance"].GetBoolean("TopMost");
        }

        #endregion
    }
}
